/**
 * Copyright 2009 the original author or authors.
 *
 * Licensed under the GPL License, Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.gnu.org/licenses/lgpl-3.0.txt
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.guardianapis.api.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.thoughtworks.xstream.converters.ConversionException;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;

public class XMLGregorianCalendarConverter implements Converter {

	/*******************************************************************************
	 * Constants
	 *******************************************************************************/

	private static Log log = LogFactory.getLog(XMLGregorianCalendarConverter.class);

	/*******************************************************************************
	 * Properties
	 *******************************************************************************/

	 private Locale locale;
	
	/*******************************************************************************
	 * Constructors
	 *******************************************************************************/

     public XMLGregorianCalendarConverter(Locale pLocale) {
         super();
         this.locale = pLocale;
     }

     /*******************************************************************************
	 * Implementing Converter
	 *******************************************************************************/

     @SuppressWarnings("unchecked")
	public boolean canConvert(Class pClazz) {
    	return Calendar.class.isAssignableFrom(pClazz);
     }

     public void marshal(Object pValue, HierarchicalStreamWriter pWriter, MarshallingContext pContext) {
    	log.debug("Attempting to marshall value: " + pValue);
		Calendar calendar = (Calendar) pValue;
		Date date = calendar.getTime();
		DateFormat formatter = DateFormat.getDateInstance(DateFormat.FULL, this.locale);
		pWriter.setValue(formatter.format(date));
     }

     public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
    	 GregorianCalendar calendar = new GregorianCalendar();
         DateFormat formatter = DateFormat.getDateInstance(DateFormat.FULL, this.locale);
         try {
        	 String dateStringValue = reader.getValue();
        	 log.debug("Raw date string value for conversion: " + dateStringValue);
         	calendar.setTime(formatter.parse(dateStringValue));
         } catch (ParseException e) {
         	throw new ConversionException(e.getMessage(), e);
         }
         return calendar;
     }

}
